<?php
class Comment extends AppModel{
	public $name='Comment';
	 
	public $validate = array(		
		'comment' => array(array('rule'=>'notEmpty','message'=>'Can not be empty!'))
		);
		
		public $belongsTo = array(	 
		 'User' => array(
		 'className' => 'User',
		 'foreignKey' => 'user_id'
		 )
		 );
		 
	public function addComment($comment='',$userid=0,$taskid=0,$isNotify=true){
		if(!empty($comment) && $taskid>0 && $userid>0){
			$data['comment']=$comment;
			$data['task_id']=$taskid;
			$data['user_id']=$userid;
			
			
			if($this->create($data) && $this->save()) {
				App::uses('TaskNotification', 'Model');
				$TaskNotification= new TaskNotification();
				$TaskNotification->addNotify($userid,$taskid);				
				if($isNotify)$TaskNotification->notify($userid,$taskid);		
				//update count in task
				$this->query('update tasks set comment_count=(select count(*) from comments where task_id='.$taskid.') where id='.$taskid);
			}
			
		}
	}
}
?>
